﻿Imports DTO
Imports BUS
Public Class ucDonViTinh

    Dim dv As New DonViDTO

    Private Sub barbtnThemDV_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemDV.ItemClick
        Dim frm As New frmThemDonVi
        AddHandler frm.LamMoiNVKhiThem, AddressOf LayDSDonVi
        frm.ShowDialog()
    End Sub

    Sub LayDSDonVi()
        GridControl1.DataSource = DonViBUS.LayDSDonVi()
    End Sub

    Private Sub ucDonViTinh_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaDV.Enabled = False
        barbtnXoaDV.Enabled = False
        barbtnXuatDV.Enabled = False
        LayDSDonVi()
    End Sub

    Private Sub barbtnSuaChuaDV_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaDV.ItemClick
        Dim frm As New frmThemDonVi
        frm.Text = "Sửa chữa đơn vị"
        frm.kt = 1
        frm.tenDV = dv.TenDonVi
        frm.maDV = dv.MaDonVi
        frm.ghichu = dv.GhiChu
        frm.maTT = dv.MaTrangThai
        frm.ShowDialog()
    End Sub

    Private Sub barbtnXoaDV_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaDV.ItemClick

        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 3)
        If pq.Xoa = True Then
            Try
                If DonViBUS.XoaDonVi(dv) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công đơn vị", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSDonVi()
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Đơn vị này đang sử dụng không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                DonViBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        
    End Sub

    Private Sub barbtnNapLaiDV_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiDV.ItemClick
        LayDSDonVi()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaDV.Enabled = True
            barbtnXoaDV.Enabled = True
            dv.MaDonVi = GridView1.GetRowCellValue(e.RowHandle, GCMaDV).ToString()
            dv.TenDonVi = GridView1.GetRowCellValue(e.RowHandle, GCTenDV).ToString()
            dv.GhiChu = GridView1.GetRowCellValue(e.RowHandle, GCGhiChuDV).ToString()
            dv.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCQuanLyDV))
        End If
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub
End Class
